// logs.js
const plugin = requirePlugin("ble_plugin");

Page({
  data: {
    lock: {},
    isLogin: false,
    userInfo: {},
    status:"0"
  },
  getInfo() {
    wx.getUserProfile({
      desc: '获取你的信息用于小程序展示。',
      success: res => {
        console.log(res.userInfo)
        this.setData({
          userInfo: res.userInfo,
          isLogin: true
        })
        wx.setStorageSync('user', res.userInfo)
      }
    })
  },
  onLoad() {
    if (wx.getStorageSync('user')) {
      this.setData({
        isLogin: true,
        userInfo: wx.getStorageSync('user')
      })
    }
    this.init()
    this.getStatus()
  },
  getStatus() {
    plugin.bleAction({
      mac: "36:63:0d:04:04:44",
      pwd: "000000",
      action: "26",
      params: "",
      callBack: res => {
        console.log("执行结果", res)
        this.setData({
          status:res.on
        })
      }
    })
  },
  openLock() {
    wx.showModal({
      title: '提示',
      content: '',
      editable: true,
      placeholderText: "请输入开锁密码",
      success: res => {
        if (res.confirm) {
          wx.showLoading({
            title:"正在开锁",
            mask:true
          })
          this.init().then(() => {
            plugin.bleAction({
              mac: "36:63:0d:04:04:44", // 锁的蓝牙地址
              pwd: res.content,
              action: "21",
              params: "",
              callBack: res => {
                console.log("执行结果", res)
                if(res.code==="0000"){
                  wx.hideLoading({
                    success: (res) => {
                        
                    },
                  })
                }
              }
            })
          })
        }
      }
    })


  },
  init() {
    return new Promise((resolve, reject) => {
      plugin.bleAction({
        mac: "36:63:0d:04:04:44",
        pwd: "00000000",
        action: "20",
        params: "",
        callBack: res => {
          console.log("执行结果", res)
          if (res.code === "0000") {
            this.setData({
              lock: res
            })
            resolve()
          }
        }
      })
    })
  }
})